package com.yiba.vpn;

import android.text.TextUtils;

import com.yiba.logutils.LogUtils;
import com.yiba.vpn.core.LocalVpnService;
import com.yiba.vpn.core.VPNNode;
import com.yiba.vpn.proxy.ProxyConfig;
import com.yiba.vpn.utils.logger.LogUtil;

/**
 * VPN管理类
 * Created by Administrator on 2017/10/27 0027.
 */

public class VPNServiceManager {
    public static final String TAG = "VPN_LOG";
    public String proxy_url;

    private static class SingletonHolder {
        private static final VPNServiceManager INSTANCE = new VPNServiceManager();
    }

    private VPNServiceManager() {
    }

    public static final VPNServiceManager getInstance() {
        return SingletonHolder.INSTANCE;
    }

    /**
     * 设置VPN的相关信息
     *
     * @param vpnNodeBean
     */
    public void setVPNConfig(VPNNode vpnNodeBean) {
        if (vpnNodeBean != null) {
            String server_port = vpnNodeBean.getServer_port();
            String server_host = vpnNodeBean.getServer();
            String method = vpnNodeBean.getMethod();
            String password = vpnNodeBean.getPassword();
            //设置vpn proxy_url
            if (!TextUtils.isEmpty(server_port) &&
                    !TextUtils.isEmpty(server_host) &&
                    !TextUtils.isEmpty(method) &&
                    !TextUtils.isEmpty(password)) {
                proxy_url = "ss://" + method + ":" + password + "@" + server_host + ":" + server_port;
                LogUtil.e("Load proxy_url:\n"+ proxy_url);
                LogUtils.getInstance().log(TAG,"获取后台节点配置：Load proxy_url:\n"+server_host+server_port);
                LocalVpnService.ProxyUrl = proxy_url;
            }
            //设置proxy pac，手机内部端口等
            VPNNode.ConfigBean config = vpnNodeBean.getConfig();
            if (config != null) {
                ProxyConfig.Instance.loadFormSerializableObj(config);
            }
//            DBQTestTimeDelay dbqTestTimeDelay = new DBQTestTimeDelay(server_host,Integer.parseInt(
//                    server_port));
//            dbqTestTimeDelay.start_UDP_TimeDelay();
        }
    }
}
